package com.ruoyi.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.domain.Booking;
import com.ruoyi.mapper.BookingMapper;
import com.ruoyi.service.IBookingService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

/**
 * <p>
 * 客户预约表 服务实现类
 * </p>
 *
 * @author 林颖
 * @since 2025-02-05
 */
@Service
public class BookingServiceImpl extends ServiceImpl<BookingMapper, Booking> implements IBookingService {
    // 解析时间参数并调用查询方法
    public List<Booking> fetchDataByDateTimeString(String dateTimeString) {
        // 定义时间格式化器
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

        // 解析时间字符串为 LocalDateTime 对象
        LocalDateTime dateTime = LocalDateTime.parse(dateTimeString, formatter);

        // 获取日期部分（年月日）
        LocalDateTime startDate = dateTime.toLocalDate().atTime(LocalTime.MIN); // 当天开始时间
        LocalDateTime endDate = dateTime.toLocalDate().atTime(LocalTime.MAX); // 当天结束时间


        // 查询当天数据
        LambdaQueryWrapper<Booking> lqw = new LambdaQueryWrapper<>();
        lqw.ge(Booking::getAppointmentTime,startDate);
        lqw.le(Booking::getAppointmentTime,endDate);
        lqw.ne(Booking::getQueueStatus,"2");//排除已完成的预约信息

        return this.list(lqw);
    }

    @Override
    public Integer getAheadCount(Booking booking) {
        // 定义日期时间格式化器
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

        List<Booking> bookings = this.fetchDataByDateTimeString(booking.getAppointmentTime());

        Integer num = 0;
        for (Booking bookingInfo : bookings) {


            // 将字符串转换为 LocalDateTime 对象
            LocalDateTime dateTime1 = LocalDateTime.parse(bookingInfo.getAppointmentTime(), formatter);
            LocalDateTime dateTime2 = LocalDateTime.parse(booking.getAppointmentTime(), formatter);

            // 比较两个日期时间
            if (dateTime1.isBefore(dateTime2)) {
                num+=1;
            }
        }

        return num;
    }

    @Override
    public Double getCurrentRate(Booking booking) {
        List<Booking> bookings = this.fetchDataByDateTimeString(booking.getAppointmentTime());

        Double aheadCount = Double.valueOf(this.getAheadCount(booking));//前方人数
        if (aheadCount == 0 || "2".equals(booking.getQueueStatus())){
            return 100.0;
        }
        Double totalCount = (double) bookings.size();//当天总人数
        Double afterCount = totalCount - aheadCount;//后方人数
        Double num = (afterCount  / totalCount) * 100;

        return num;
    }

    //转换排队状态
    @Override
    public String tranQueueStatus(String queueStatus) {
        if ("0".equals(queueStatus)){
            return "排队中";
        }else if ("1".equals(queueStatus)){
            return "就诊中";
        }else {
            return "已完成";
        }
    }

    @Override
    public String tranGender(String gender) {
        if ("0".equals(gender)){
            return "男";
        }else {
            return "女";
        }
    }
}
